package org.jeecg.modules.providers.controller;

import java.util.Arrays;
import java.util.Objects;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.security.OAuthFlow;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.providers.entity.MtServiceProviders;
import org.jeecg.modules.providers.service.IMtServiceProvidersService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 服务商表
 * @Author: jeecg-boot
 * @Date: 2022-08-05
 * @Version: V1.0
 */
@Api(tags = "服务商表")
@RestController
@RequestMapping("/providers/mtServiceProviders")
@Slf4j
public class MtServiceProvidersController extends JeecgController<MtServiceProviders, IMtServiceProvidersService> {
    @Autowired
    private IMtServiceProvidersService mtServiceProvidersService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 分页列表查询
     *
     * @param mtServiceProviders
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "服务商表-分页列表查询")
    @ApiOperation(value = "服务商表-分页列表查询", notes = "服务商表-分页列表查询")
    @GetMapping(value = "/list")
    @PermissionData(pageComponent = "providers/MtServiceProvidersList")
    public Result<IPage<MtServiceProviders>> queryPageList(MtServiceProviders mtServiceProviders,
                                                           @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                           HttpServletRequest req) {
        QueryWrapper<MtServiceProviders> queryWrapper = QueryGenerator.initQueryWrapper(mtServiceProviders, req.getParameterMap());
        Page<MtServiceProviders> page = new Page<MtServiceProviders>(pageNo, pageSize);
        IPage<MtServiceProviders> pageList = mtServiceProvidersService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param mtServiceProviders
     * @return
     */
    @AutoLog(value = "服务商表-添加")
    @ApiOperation(value = "服务商表-添加", notes = "服务商表-添加")
    @RequiresPermissions("providers:add")
    @PostMapping(value = "/add")
    @Transactional(rollbackFor = Exception.class)
    public Result<String> add(@RequestBody MtServiceProviders mtServiceProviders) {
        //mtServiceProvidersService.save(mtServiceProviders);
        //服务商添加  添加用户 添加部门 关联角色
        mtServiceProvidersService.saveServiceProvidersAndCreateUser(mtServiceProviders);


        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param mtServiceProviders
     * @return
     */
    @AutoLog(value = "服务商表-编辑")
    @ApiOperation(value = "服务商表-编辑", notes = "服务商表-编辑")
    @RequiresPermissions("providers:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody MtServiceProviders mtServiceProviders) {
        mtServiceProvidersService.updateById(mtServiceProviders);
        return Result.OK("编辑成功!");
    }

    /**
     * 地图标注
     *
     * @param mtServiceProviders
     * @return
     */
    @AutoLog(value = "服务商表-地图标注")
    @ApiOperation(value = "服务商表-地图标注", notes = "服务商表-地图标注")
    @RequiresPermissions("providers:coordinate")
    @RequestMapping(value = "/coordinate", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> coordinate(@RequestBody MtServiceProviders mtServiceProviders) {
        mtServiceProvidersService.updateById(mtServiceProviders);
        return Result.OK("地图标注成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "服务商表-通过id删除")
    @ApiOperation(value = "服务商表-通过id删除", notes = "服务商表-通过id删除")
    @RequiresPermissions("providers:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        mtServiceProvidersService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "服务商表-批量删除")
    @ApiOperation(value = "服务商表-批量删除", notes = "服务商表-批量删除")
    @RequiresPermissions("providers:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.mtServiceProvidersService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "服务商表-通过id查询")
    @ApiOperation(value = "服务商表-通过id查询", notes = "服务商表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<MtServiceProviders> queryById(@RequestParam(name = "id", required = true) String id) {
        MtServiceProviders mtServiceProviders = mtServiceProvidersService.getById(id);
        if (mtServiceProviders == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(mtServiceProviders);
    }


    /**
     * 查询登录服务商信息
     *
     * @return
     */
    @ApiOperation(value = "服务商表-查询登录服务商信息", notes = "服务商表-查询登录服务商信息")
    @GetMapping(value = "/queryInfo")
    public Result<MtServiceProviders> queryInfoById() {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        SysUser sysUser = sysUserService.getById(loginUser.getId());
        // 非服务商用户
        if (sysUser.getUserType() != 1) {
            return Result.OK();
        }
        MtServiceProviders mtServiceProviders = mtServiceProvidersService.getOne(new LambdaQueryWrapper<MtServiceProviders>()
                .eq(MtServiceProviders::getUserId, loginUser.getUsername()));
        if (Objects.isNull(mtServiceProviders)) {
            return Result.error("未找到对应服务商");
        }
        return Result.OK(mtServiceProviders);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param mtServiceProviders
     */
    @RequiresPermissions("providers:export")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, MtServiceProviders mtServiceProviders) {
        return super.exportXls(request, mtServiceProviders, MtServiceProviders.class, "服务商表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequiresPermissions("providers:import")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, MtServiceProviders.class);
    }

}
